package com.example.woke;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.bean.BaseResponse;
import com.http.HttpMethods;
import com.jakewharton.rxbinding2.view.RxView;
import com.woke.adapter.UnionPayChannelMultiSelectAdapter;
import com.woke.data.UnionPayChannel;
import com.zhongcai.online.R;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 激活商家收款码
 * 和 收款码通道（商家专用收款码通道）
 */
public class ChannelActivateActivity extends AppCompatActivity {

	private RecyclerView mChannelRecyView;
	private UnionPayChannelMultiSelectAdapter mChannelAdapter;
	private ArrayList<UnionPayChannel> mUnionPayChannels = new ArrayList<> ();
	private Button mSureBtn;
	private int mUserId = - 1;
	private String mSerialNum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_channel_activate);
		mUserId = getIntent ().getIntExtra ("userId", - 1);
		mSerialNum = getIntent ().getStringExtra ("serialNum");
		if (mUserId == - 1 || mUserId == 0) {
			throw new IllegalArgumentException ("userId no to -1 or 0");
		}
		Log.e ("---", "userId:" + mUserId + "--SerialNo=" + mSerialNum);
		initToolbar ();
		initOtherView ();
	}

	@Override
	protected void onResume() {
		super.onResume ();
		getMerchatCollectionChannel ();
	}

	private void initToolbar() {
		Toolbar toolbar = (Toolbar) findViewById (R.id.toolbar);
		toolbar.setNavigationOnClickListener (new View.OnClickListener () {
			@Override
			public void onClick(View v) {
				finish ();
			}
		});
//		setSupportActionBar (toolbar);
	}

	private void initOtherView() {
		mChannelRecyView = (RecyclerView) findViewById (R.id.channel_recyclerView);
		LinearLayoutManager layoutManager = new LinearLayoutManager (this, LinearLayoutManager.VERTICAL, false);
		layoutManager.setSmoothScrollbarEnabled (true);
		layoutManager.setAutoMeasureEnabled (true);
		mChannelRecyView.setLayoutManager (layoutManager);
		mChannelRecyView.setHasFixedSize (true);
		mChannelRecyView.setNestedScrollingEnabled (false);
		mChannelAdapter = new UnionPayChannelMultiSelectAdapter (ChannelActivateActivity.this, mUnionPayChannels);
		mChannelRecyView.setAdapter (mChannelAdapter);

		mSureBtn = (Button) findViewById (R.id.channel_sure_pay);
		RxView.clicks (mSureBtn)
				.throttleFirst (2, TimeUnit.SECONDS)
				.subscribe (new Consumer<Object> () {
					@Override
					public void accept(Object o) throws Exception {
						activateSerierNoAndOpenMultiChannel ();
					}
				});
	}

	/**
	 * 获取商家收款码渠道
	 */
	private void getMerchatCollectionChannel() {
		HttpMethods.getInstance ().getMerchatCollectionChannel (mChannels, mUserId, mSerialNum);
	}

	/**
	 * 激活商家收款码并开通多个渠道
	 */
	private void activateSerierNoAndOpenMultiChannel() {

		Log.e ("ChannelActivate", "------------------");
		if (mChannelAdapter != null) {
			int[] ways = mChannelAdapter.getWay ();


			if (ways != null && ways.length > 0) {
				StringBuilder sb = new StringBuilder ();
				for (int way : ways) {
					sb.append (way).append (",");
				}
				String gallery = sb.substring (0, sb.length () - 1);//弃掉最后一个逗号,
				Log.e ("ChannelActivate", "----通道----" + gallery);
				HttpMethods.getInstance ()
						.openChannelAndActivateSerierNo (mActivateAndOpenGalley, mUserId, mSerialNum, gallery);
			}
		}
//		Log.e ("ChannelActivate", "------------------");

	}

	/**
	 * 渠道
	 */
	private Observer<List<UnionPayChannel>> mChannels = new Observer<List<UnionPayChannel>> () {
		@Override
		public void onSubscribe(Disposable d) {

		}

		@Override
		public void onNext(List<UnionPayChannel> value) {
			if (value != null && value.size () > 0) {
				Log.e ("ChannelActivate", "---" + value.toString ());
				mUnionPayChannels.clear ();
				mUnionPayChannels.addAll (value);
				if (mChannelAdapter != null) {
					mChannelAdapter.notifyDataSetChanged ();
				}
			} else {
				Log.e ("ChannelActivate", "渠道为空");
			}

		}

		@Override
		public void onError(Throwable e) {
			Log.e ("ChannelActivate", e.getMessage ());
			Toast.makeText (ChannelActivateActivity.this, e.getMessage (), Toast.LENGTH_SHORT)
					.show ();
		}

		@Override
		public void onComplete() {

		}
	};

	private Observer<BaseResponse> mActivateAndOpenGalley = new Observer<BaseResponse> () {
		@Override
		public void onSubscribe(Disposable d) {

		}

		@Override
		public void onNext(BaseResponse value) {

			if (value != null) {
				Log.e ("onNext",value.toString ());
				Toast.makeText (ChannelActivateActivity.this, value.getInfo (), Toast.LENGTH_SHORT)
						.show ();
				if (value.resultSuccess ()) {
					finish ();
				}
			}
		}

		@Override
		public void onError(Throwable e) {
			Log.e ("onNext",e.getMessage ());
			Toast.makeText (ChannelActivateActivity.this, e.getMessage (), Toast.LENGTH_SHORT)
					.show ();
		}

		@Override
		public void onComplete() {

		}
	};
}
